﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Jenzabar.Portal.Framework.Web.UI;
using Jenzabar.Portal.Framework.Security.Authorization;// Add reference Jenzabar.Portal.Framework.dll for operations
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Data; 
using Framework = Jenzabar.Portal.Framework.Configuration.Settings;
using System.Configuration;// used to find EX db connection value
using System.Net.Mail;

using System.Diagnostics;
using Jenzabar.Portal.Framework;


namespace Portlet.RHFSContract
{

    #region Operations
    //[PortletOperation("CanAdminPortlet", "Can Administer Portlet", "Whether user can administer the portlet", PortletOperationScope.Portlet)]
    [PortletOperation("CANVIEWCONTRACT", "AU_ResidenceContract_CAN_VIEW_CONTRACT", "AU_ResidenceContract_CAN_VIEW_CONTRACT", PortletOperationScope.Global)]
    [PortletOperation("CANADMINCONTRACT", "AU_ResidenceContract_CAN_ADMIN_CONTRACT", "AU_ResidenceContract_CAN_ADMIN_CONTRACT", PortletOperationScope.Global)]
    
    #endregion Operations



    public class Contract :PortletBase
    {

        protected override PortletViewBase GetCurrentScreen()
        {
            PortletViewBase screen = null;
            switch (this.CurrentPortletScreenName)
            {
                case "Add":
                    screen = this.LoadPortletView("ICS/Portlet.RHFSContract/Admin_AddNew_View.ascx");
                    this.Tabs.SelectedIndex = 2;
                    break;
                
                case "Admin":
                    screen = this.LoadPortletView("ICS/Portlet.RHFSContract/Admin_View.ascx");
                    this.Tabs.SelectedIndex = 1;
                    break;
                case "thanks":
                    screen = this.LoadPortletView("ICS/Portlet.RHFSContract/thanks_view.ascx");
                    break;
                case "New":
                    screen = this.LoadPortletView("ICS/Portlet.RHFSContract/Default_View.ascx");
                   // this.Tabs.SelectedIndex = 1; //Already gave tab index
                    break;
                default :
                    screen = this.LoadPortletView("ICS/Portlet.RHFSContract/Default_View.ascx");
                    this.Tabs.SelectedIndex = 0;
                    break;

            }
            return screen;

        }


        protected override void OnInit(EventArgs e)
        {
            Toolbar.ItemCommand += new CommandEventHandler(Toolbar_ItemCommand);
            
            base.OnInit(e);
        }

        void Toolbar_ItemCommand(object sender, CommandEventArgs e)
        {
            this.NextScreen(e.CommandName);
        }

        protected override bool PopulateTabs(Jenzabar.Common.Web.UI.Controls.SuperTabs tabs)
        {
            bool showTabs = false;
            bool isAdmin = this.AccessCheck("CANADMINCONTRACT");
            if (this.AccessCheck("CANVIEWCONTRACT") || isAdmin)
            {
               
                tabs.Tabs.Add("New", "New", "New");
              
                showTabs = true;
            }
            if (isAdmin)
            {
              
                tabs.Tabs.Add("Admin", "Administrator", "Admin");
                //tabs.Tabs.Add("Add", "Add New", "Add New");
            }
            return showTabs;
        }






    public static void PopulateMajor(DropDownList dd)
    {
        SqlDataReader reader;
        //SqlConnection conn = new SqlConnection(ConnectionString);
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NotepadConnectionString"].ConnectionString);
        // Both cmd are working
        //SqlCommand cmd = new SqlCommand(" SELECT DISTINCT address_master.state FROM address_master", conn); // working
       // SqlCommand cmd = new SqlCommand("SELECT list_value, list_label FROM dbo.avila_majors order by list_label ", conn);
        //SqlCommand cmd = new SqlCommand("SELECT pcf.PROG_CDE, pdf.prog_desc FROM program_config pcf inner join  program_def pdf on pdf.prog_cde = pcf.prog_cde   where pcf.DIV_CDE ='UG' and pdf.SHOW_ON_WEB='Y' and pcf.CANDIDACY_TYPE='F' ORDER BY pdf.prog_desc ASC ", conn);
        SqlCommand cmd = new SqlCommand("CUS_sp_FRM_PopulateMajor", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        try
        {
            conn.Open();
            reader = cmd.ExecuteReader();
            dd.DataSource = reader;
            dd.DataValueField = "PROG_CDE"; //"list_value";
            dd.DataTextField = "prog_desc";//"list_label";
            dd.DataBind();
            reader.Close();

        }
        finally
        {
            conn.Close();

        }

    }

         public static void sendMail(string body, string from, string to, string subject)
         {
             string _body = body.ToString();
             // string delim = ";";
             SmtpClient MailObj = new SmtpClient("mail.avila.edu");
             MailMessage msg = new MailMessage();
             msg.From = new MailAddress(from.ToString());
             msg.To.Add(new MailAddress(to.ToString()));
             msg.Subject = subject.ToString();
             msg.Body = _body.ToString();
             msg.IsBodyHtml = true;
             MailObj.Send(msg);
         }

         string gender, dob ;
         public string findDOB(string id)
         {
             
             SqlConnection conn;
             using (conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NotepadConnectionString"].ConnectionString))
             {
                 SqlCommand cmd = new SqlCommand("CUS_sp_FindBio", conn);
                 cmd.CommandType = CommandType.StoredProcedure;

                 SqlDataReader rdr;

                 try
                 {
                     conn.Open();
                     //cmd.Parameters.Add("id", SqlDbType.Int).Value = Convert.ToInt32(ID); // wrong age
                     cmd.Parameters.Add("id", SqlDbType.VarChar).Value = id;//this will give correct age for dev disable this
                     rdr = cmd.ExecuteReader();
                     while (rdr.Read())
                     {

                         dob = rdr["dob"].ToString();

                     }

                     return dob;

                     //rdr.Close();
                 }
                 catch (Exception ex)
                 {
                     string err = ex.Message;
                     return string.Empty;
                     // response.Write("Error retreiving Data Items: " + ex.Message);
                 }
                 finally
                 {

                     conn.Close();

                 }

             }

             
         }
         public string findGender(string id)
         {

             SqlConnection conn;
             using (conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NotepadConnectionString"].ConnectionString))
             {
                 SqlCommand cmd = new SqlCommand("CUS_sp_FindBio", conn);
                 cmd.CommandType = CommandType.StoredProcedure;

                 SqlDataReader rdr;

                 try
                 {
                     conn.Open();
                    // cmd.Parameters.Add("id", SqlDbType.Int).Value = Convert.ToInt32(id);
                     cmd.Parameters.Add("id", SqlDbType.VarChar).Value = id;
                     rdr = cmd.ExecuteReader();
                     while (rdr.Read())
                     {

                         gender = rdr["GENDER"].ToString();

                     }

                     return gender;

                     //rdr.Close();
                 }
                 catch (Exception ex)
                 {
                     string err = ex.Message;
                     return string.Empty;
                     // response.Write("Error retreiving Data Items: " + ex.Message);
                 }
                 finally
                 {

                     conn.Close();

                 }

             }


         }

        


         //Other Method to find age ---I am using different method in Default_View.ascx.cs
         public string findAge(string id)
         {

             SqlConnection conn;
             using (conn = new SqlConnection(ConfigurationManager.ConnectionStrings["NotepadConnectionString"].ConnectionString))
             {
                 SqlCommand cmd = new SqlCommand("CUS_sp_FindBio", conn);
                 cmd.CommandType = CommandType.StoredProcedure;

                 SqlDataReader rdr;

                 try
                 {
                     conn.Open();
                     cmd.Parameters.Add("id", SqlDbType.Int).Value = Convert.ToInt32(ID);
                     rdr = cmd.ExecuteReader();
                     while (rdr.Read())
                     {

                         gender = rdr["GENDER"].ToString();

                     }

                     return gender;

                     //rdr.Close();
                 }
                 catch (Exception ex)
                 {
                     string err = ex.Message;
                     return string.Empty;
                     // response.Write("Error retreiving Data Items: " + ex.Message);
                 }
                 finally
                 {

                     conn.Close();

                 }

             }


         }
    }
}
